// 相机
import * as Cesium from 'cesium'
import config from './config.js'

export default {
  init(viewer, setting) {
    this._viewer = viewer
    this._viewer.scene.globe.depthTestAgainstTerrain = false // 图标被遮挡
    setting = setting || {}
    // 设置后当相机高度达到设置的最大和最小高度时将不再放大和缩小
    viewer.scene.screenSpaceCameraController.minimumZoomDistance = setting.minimumZoomDistance || config.camera.minimumZoomDistance // 相机的高度的最小值
    viewer.scene.screenSpaceCameraController.maximumZoomDistance = setting.maximumZoomDistance || config.camera.maximumZoomDistance // 相机高度的最大值
    // 默认坐标设置
    let destination = setting.destination || {}
    destination = Object.assign({}, config.destination, destination)
    let orientation = setting.orientation || {}
    orientation = Object.assign({}, config.orientation, orientation)
    // 飞行至目标
    this.flyToPoint(destination.lng, destination.lat, destination.height, undefined, orientation.heading, orientation.pitch, orientation.roll)
  },
  /**
   * @description: 飞行定位到一个矩形
   * @param {Array.<Cartesian3>} cartesians 笛卡尔坐标数组 Array.<Cartesian3>
   * @param {Number} heading  =0.0   偏航角 正北,由正北向东偏向为正
   * @param {*} pitch  =-90     俯仰角  =-90 ENU局部坐标系，XY平面的旋转角度，平面下为负，上为正，
   * @param {*} scale    =1.0   范围缩放倍率
   * @param {*} duration =3   持续时间
   * @param {*} callBack =null   回调函数，定位完成后执行
   * @return {*}
   */
  flyToRectangle(cartesians, duration = 3, heading = 0.0, pitch = -90, scale = 1.0, callBack = null) {
      if (!this._viewer) {
          return;
      }
      if (!Array.isArray(cartesians)) {
          return;
      }
      if (scale < 0.1) {
          scale = 1.0;
      }
      var rec = Cesium.Rectangle.fromCartesianArray(cartesians);
      var boundingSphere = Cesium.BoundingSphere.fromRectangle3D(rec);
      boundingSphere.radius = boundingSphere.radius * scale;
      this._viewer.camera.flyToBoundingSphere(boundingSphere, {
          duration: duration,
          maximumHeight: undefined,
          complete: function() {
              if (callBack) {
                  callBack();
              }
          },
          cancel: function() {},
          offset: {
              heading: Cesium.Math.toRadians(heading),
              pitch: Cesium.Math.toRadians(pitch),
              range: 0.0
          }
      });
  },

  /**
   * @description: 飞行定位到Entity
   * @param {Array.<Cartesian3>} cartesians 笛卡尔坐标数组 Array.<Cartesian3>
   * @param {Number} heading  =0.0   偏航角 正北,由正北向东偏向为正
   * @param {*} pitch  =-90     俯仰角  =-90 ENU局部坐标系，XY平面的旋转角度，平面下为负，上为正，
   * @param {*} duration =3   持续时间
   * @return {*}
   */
  zoomToEntity(entity, heading = 0.0, pitch = -90) {
      if (!this._viewer) {
          return;
      }
      this._viewer.zoomTo(entity, new Cesium.HeadingPitchRange(heading, pitch, 0));
  },

  /**
   * @description: 飞行定位到Entity
   * @param {Array.<Cartesian3>} cartesians 笛卡尔坐标数组 Array.<Cartesian3>
   * @param {Number} heading  =0.0   偏航角 正北,由正北向东偏向为正
   * @param {*} pitch  =-90     俯仰角  =-90 ENU局部坐标系，XY平面的旋转角度，平面下为负，上为正，
   * @param {*} duration =3   持续时间
   * @return {*}
   */
  flyToEntity(entity, duration = 3, heading = 0.0, pitch = -90) {
      if (!this._viewer) {
          return;
      }
      this._viewer.flyTo(entity, {
          duration: duration,
          offset: {
              heading: Cesium.Math.toRadians(heading),
              pitch: Cesium.Math.toRadians(pitch),
              range: 0.0
          }
      });
  },
  /**
   * @description: 飞行定位到点位坐标
   * @param {Array.<Cartesian3>} cartesians 笛卡尔坐标数组 Array.<Cartesian3>
   * @param {Number} heading  =0.0   偏航角 正北,由正北向东偏向为正
   * @param {*} pitch  =-90     俯仰角  =-90 ENU局部坐标系，XY平面的旋转角度，平面下为负，上为正，
   * @param {*} duration =3   持续时间
   * @return {*}
   */
  flyToPoint(lng, lat, height = 100000, duration = 3, heading = 0.0, pitch = -90, roll = 0) {
    if (!this._viewer) {
      return
    }
    this._viewer.camera.flyTo({
      destination: Cesium.Cartesian3.fromDegrees(lng, lat, height),
      duration: duration,
      orientation: {
        heading: Cesium.Math.toRadians(heading), // 水平偏角
        pitch: Cesium.Math.toRadians(pitch), // 俯视角
        roll: Cesium.Math.toRadians(roll) // 旋转角
      }
    })
  },
  // 显示图片hdf_
  showPoint(lng, lat, img) {
    if (!this._viewer) {
      return
    }
    this._viewer.entities.add({
      position: Cesium.Cartesian3.fromDegrees(lng, lat),
      point: {
        show: false, // default
        color: Cesium.Color.SKYBLUE, // default: WHITE
        pixelSize: 10, // default: 1
        outlineColor: Cesium.Color.YELLOW, // default: BLACK
        outlineWidth: 1 // default: 0
      },
      billboard: {
        image: img,
        width: 100,
        height: 100
      }
    })
  },
  // 添加面hdf_
  addCuboid(points) {
    //
    const positions = Cesium.Cartesian3.fromDegreesArray(points)
    const hole = Cesium.Cartesian3.fromDegreesArray([0, 0, 0, 0, 0, 0, 0, 0])
    // 定义需要传给hierrchy的参数
    const x = {
        positions: positions,
        holes: [{ positions: hole }]
    }
    const cuboid = this._viewer.entities.add({
        name: '555',
        polygon: {
            hierarchy: x,
            height: 10000,
            outline: true,
            outlinewidth: 3000,
            outlineColor: Cesium.Color.RED.withAlpha(0.8),
            // arcType: Cesium.ArcType.RHUMB,
            material: Cesium.Color.RED.withAlpha(0)
        }
    })
    // this._viewer.zoomTo(cuboid) 0.0, pitch = -90
    this._viewer.zoomTo(cuboid, new Cesium.HeadingPitchRange(0.0, -90, 0));
  },
  // 删除所有实体对象hdf_
  clearMap() {
    this._viewer.entities.removeAll()
  },
  // 重置
  resetZoom() {
    const info = this.get_camera_height();
    this._viewer.camera.flyTo({
        destination: Cesium.Cartesian3.fromDegrees(
            info.centerLon,
            info.centerLat,
            22800000
        ),
        duration: 1.0
    })
  },
  // 获取当前相机高经弹纬度
  get_camera_height() {
    var viewer = this._viewer;
    var cameraPosition = viewer.camera.position;
    var ellipsoid = viewer.scene.globe.ellipsoid;
    var cartographic = ellipsoid.cartesianToCartographic(cameraPosition);
    // 获取镜头高度
    var height = cartographic.height
    var centerLon = parseFloat(
      Cesium.Math.toDegrees(cartographic.longitude).toFixed(8)
    )
    var centerLat = parseFloat(
      Cesium.Math.toDegrees(cartographic.latitude).toFixed(8)
    )
    return { height, centerLon, centerLat }
  },

  /**
   * @description: 飞行定位到矩形框位置
   * @param {Array.<Cartesian3>} cartesians 笛卡尔坐标数组 Array.<Cartesian3>
   * @param {Number} heading  =0.0   偏航角 正北,由正北向东偏向为正
   * @param {*} pitch  =-90     俯仰角  =-90 ENU局部坐标系，XY平面的旋转角度，平面下为负，上为正，
   * @param {*} duration =3   持续时间
   * @return {*}
   */
  flyToBoundingSphere(boundingSphere, heading = 0.0, pitch = -45) {
    if (!this._viewer) {
      return
    }
    this._viewer.camera.viewBoundingSphere(boundingSphere, new Cesium.HeadingPitchRange(heading, pitch, 0))
    this._viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY)
  },
  // 获取相机坐标
  getDestination (viewer) {
    var cartographic = viewer.scene.camera.positionCartographic  // with longitude and latitude expressed in radians and height in meters.
    // 以下方式也可以获取相机位置只是返回的坐标系不一样
    // var position = viewer.scene.camera.position // cartesian3 空间直角坐标系
    // var cartographic = scene.globe.ellipsoid.cartesianToCartographic(position)
    var longitude = parseFloat(Cesium.Math.toDegrees(cartographic.longitude).toFixed(4))
    var latitude = parseFloat(Cesium.Math.toDegrees(cartographic.latitude).toFixed(4))
    var height = parseFloat(cartographic.height.toFixed(3))
    return { longitude, latitude, height }
  },
  // 获取相机姿态
  getOrientation (viewer) {
    var heading = parseFloat(Cesium.Math.toDegrees(viewer.camera.heading).toFixed(2))
    var pitch = parseFloat(Cesium.Math.toDegrees(viewer.camera.pitch).toFixed(2))
    var roll = parseFloat(Cesium.Math.toDegrees(viewer.camera.roll).toFixed(2))
    return { heading, pitch, roll }
  }
}
